﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using rehabilitation.Data;
using rehabilitation.Models;

namespace rehabilitation.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class GuidanceController : ControllerBase
    {
        private readonly AppDbContext _context;

        public GuidanceController(AppDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 1. 根据医生ID查询康复指导列表（分页）
        /// </summary>
        [HttpGet("listByDoctor/{doctorId}")]
        public async Task<IActionResult> GetGuidanceByDoctor(int doctorId, int pageIndex = 1, int pageSize = 10)
        {
            if (pageIndex < 1 || pageSize < 1)
            {
                return BadRequest("分页参数无效");
            }

            var query = _context.Guidances
                .Where(g => g.DoctorId == doctorId)
                .OrderByDescending(g => g.CreateTime);

            int totalRecords = await query.CountAsync();
            var guidanceList = await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();

            return Ok(new
            {
                TotalRecords = totalRecords,
                PageIndex = pageIndex,
                PageSize = pageSize,
                Data = guidanceList
            });
        }

        /// <summary>
        /// 2. 根据ID查询康复指导详情
        /// </summary>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetGuidanceById(int id)
        {
            var guidance = await _context.Guidances.FindAsync(id);
            if (guidance == null)
            {
                return NotFound("康复指导记录不存在");
            }
            return Ok(guidance);
        }

        /// <summary>
        /// 3. 编辑康复指导
        /// </summary>
        [HttpPut("update")]
        public async Task<IActionResult> UpdateGuidance([FromBody] Guidance guidance)
        {
            var existingGuidance = await _context.Guidances.FindAsync(guidance.Id);
            if (existingGuidance == null)
            {
                return NotFound("康复指导记录不存在");
            }

            existingGuidance.Content = guidance.Content;
            existingGuidance.CreateTime = guidance.CreateTime;

            await _context.SaveChangesAsync();
            return Ok("康复指导已更新");
        }

        /// <summary>
        /// 4. 新增康复指导
        /// </summary>
        [HttpPost("add")]
        public async Task<IActionResult> AddGuidance([FromBody] Guidance guidance)
        {
            _context.Guidances.Add(guidance);
            await _context.SaveChangesAsync();
            return Ok("康复指导已新增");
        }

        /// <summary>
        /// 5. 删除康复指导
        /// </summary>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteGuidance(int id)
        {
            var guidance = await _context.Guidances.FindAsync(id);
            if (guidance == null)
            {
                return NotFound("康复指导记录不存在");
            }

            _context.Guidances.Remove(guidance);
            await _context.SaveChangesAsync();
            return Ok("康复指导已删除");
        }
    }
}
